
var util = require('../../utils/util.js');
var url = require("../../utils/url.js");
var common = require("../../utils/common.js");
var roomId;
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    roomType: '',
    info: '',
    remark: '',
    price: '',
    isReserve: false,
    isFull: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    roomId = options.roomId;
    that.getRoomDetail(roomId);
  },

  getRoomDetail: function (roomId) {
    common.showLoading();
    wx.request({
      url: url.ROOM_DETAIL,
      method: 'POST',
      data: { id: roomId },
      success: function (res) {
        common.closeLoading();
        console.log(res.data);
        var arrs = [];
        var entity = res.data.data;
        for (var i = 0; i < entity.imgs.split(',').length; i++) {
          arrs.push(entity.imgs.split(',')[i])
        };
        that.setData({
          imgUrls: arrs,
          roomType: that.getRoomTypeName(entity.type),
          info: that.getInfo(entity),
          remark: entity.remark,
          price: entity.price,
        });
        if (entity.state == 0) {
          that.setData({
            isFull: false,
            isReserve: true
          })
        } else if (entity.state == 1) {
          that.setData({
            isFull: true,
            isReserve: false
          })
        }
      }, fail: function () {
        common.loadingError();
      }
    })
  },

  getInfo: function (entity) {
    return entity.breakfast + '、'
      + entity.floor + '层、'
      + entity.area + '㎡、'
      + entity.bed + 'm床'
  },

  isCanReserve: function (state) {
    if (state == 0) {
      return false;
    } else if (state == 1) {
      return true;
    }
  },

  getRoomTypeName: function (type) {
    if (type == 1) {
      return '单人间'
    }
    if (type == 2) {
      return '标准间'
    }
    if (type == 3) {
      return '商务间'
    }
    if (type == 4) {
      return '豪华间'
    }
  },

  /**
   * 立即预定
   */
  bindReserveNow: function (e) {
    var params = JSON.stringify({
      id: roomId,
      img: that.data.imgUrls[0],
      roomType: that.data.roomType,
      info: that.data.info,
      price: that.data.price
    });
    wx.navigateTo({
      url: '../../pages/writeOrderForm/writeOrderForm?json=' + params,
    })
  }


})